package gbench.appdemo.mall.appitf.im;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import gbench.appdemo.mall.data.MallDataApp;
import gbench.appdemo.mall.mvc.annotation.IRecordParam;

import java.util.List;
import gbench.common.tree.LittleTree.IRecord;
import gbench.common.tree.LittleTree.SQL;

import static gbench.common.tree.LittleTree.IRecord.*;

/**
 * 用户信息
 * 
 * @author gbench
 *
 */
@RequestMapping("/im/user")
@RestController
public class UserController {

    @RequestMapping("get")
    public List<IRecord> get() {
        return dataApp.sqlquery("select * from t_user");
    }

    @RequestMapping("update")
    public List<IRecord> update(@IRecordParam IRecord rec) {
        System.out.println("update 请求参数:"+rec);
        rec.computeIfPresent("icon", e->(e+"").replace("\\", "/"));// 路径名替换
        dataApp.withTransaction(sess->{
            final var sql = SQL.of("t_user",rec.aoks2rec(e->{
            switch(e) {// 键名变换
                    case "accid":return "id";
                    case "icon":return "path";
                    default: return e;
            }}));
            final var s = sql.update2();
            sess.sqlexecute(s);
        });
        return dataApp.sqlquery("select * from t_user");
    }

    @RequestMapping("delete")
    public List<IRecord> delete() {
        return dataApp.sqlquery("select * from t_user");
    }

    /**
     * 获取用户信息
     * @return
     */
    @RequestMapping("get_uinfos")
    public IRecord get_uinfos() {
        return REC("code", 200, "uinfos",
        dataApp.sqlquery("select * from t_user").stream()
        .map(e -> REC(
            "valid",true,
            "accid", e.get("id"), 
            "name", e.get("name"), 
            "icon", e.get("path"), 
            "sign", "-",
            "email", "-", 
            "birth", "-", 
            "mobile", "-", 
            "gender", 0, 
            "mute",0,
            "ex", "-")));
    }

    public MallDataApp dataApp = new MallDataApp("db1");
}
